﻿using System;
using System.Runtime.Serialization;
using System.Data.SqlClient;

namespace PMS.Exceptions
{
    /// Summary description for DataAccessException.
    /// </summary>
    [Serializable]
    public class DataAccessException : ApplicationException
    {
        private int errorCode = 0;

        public static DataAccessException GetException(string message, Exception ex)
        {
            // log this exception
            Logger.Error(ex);

            //return new DataAccessException(message, ex);
            if (ex is FormatException)
            {
                return new DataAccessException("ErrorFormat", ex);
            }
            else if (ex is OverflowException)
            {
                return new DataAccessException("ErrorOverFlow", ex);
            }
            else if (ex is NullReferenceException)
            {
                return new DataAccessException("ErrorNullReference", ex);
            }
            else if (ex is System.Data.SqlTypes.SqlTypeException)
            {
                return new DataAccessException("ErrorSqlType", ex);
            }
            //DuyHB1
            else if (ex is DataConflictException)
            {
                if (message.ToLower().Contains("update"))
                    return new DataAccessConflictUpdateException("ConflictUpdate", ex);
                return new DataAccessConflictUpdateException("ConflictDelete", ex);
            }
            else if (ex is SqlException && (ex as SqlException).Number == 547 && ex.Message.Contains("DELETE"))
            {
                return new DataAccessException(ex.Message, ex) { ErrorCode = 547 };
            }
            else if (ex is SqlException && (ex as SqlException).Number == 547 && ex.Message.Contains("FOREIGN KEY"))
            {
                return new DataAccessException(ex.Message, ex) { ErrorCode = 548 };
            }

            return new DataAccessException(ex.Message, ex);
        }

        public override string Message
        {
            get
            {
                if (!string.IsNullOrEmpty(base.Message))
                    return base.Message;
                else if (base.InnerException != null)
                    return base.InnerException.Message;

                return base.Message;
            }
        }

        protected DataAccessException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        protected DataAccessException(string message)
            : base(message)
        {
        }

        protected DataAccessException(string message, int errorCode)
            : base(message)
        {
            this.errorCode = errorCode;
        }

        protected DataAccessException(string message, Exception innerException)
            : base(message, innerException)
        {
            if (innerException is SqlException)
            {
                //this.ErrorCode = ((SqlException)innerException).ErrorCode;
                //NganNT
                this.ErrorCode = (innerException as SqlException).Number;
            }
            if (innerException is FormatException)
            {
                this.ErrorCode = 0;
            }
            else if (innerException is OverflowException)
            {
                this.ErrorCode = 0;
            }
            else if (innerException is NullReferenceException)
            {
                this.ErrorCode = 0;
            }
            else if (innerException is System.Data.SqlTypes.SqlTypeException)
            {
                this.ErrorCode = 0;
            }
        }

        public int ErrorCode
        {
            get { return errorCode; }
            set { this.errorCode = value; }
        }
    }
}
